/**
 * Classname :ContextHelper.java
 */
package com.astrocsr.common.helper;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.security.authentication.AuthenticationTrustResolver;
import org.springframework.security.authentication.AuthenticationTrustResolverImpl;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;

import com.astrocsr.domain.web.UserInfo;
import com.astrocsr.security.AppWebUserDetails;
import com.astrocsr.security.WebUserDetails;


/**
 * @author ho.viet.duan
 * Created Dec 28, 2013
 */
public abstract class ContextHelper {

	static final Logger log = Logger.getLogger(ContextHelper.class);

    /**
     * 
     * @return
     */
    public static String getLoginId() {
        try {
            return SecurityContextHolder.getContext().getAuthentication().getName();
        } catch (Exception e) {
            log.debug("getLoginId() - " + e.getMessage(), e);
        }
        return null;
    }
    
	public static UserInfo getLoginDetail() {

		UserInfo loginDetail = null;
		try {
			WebUserDetails webUserDetails = ContextHelper.getWebUserDetails();
			if (webUserDetails != null) {
				AppWebUserDetails appWebUserDetails = (AppWebUserDetails) webUserDetails;
				if (appWebUserDetails != null) {
					loginDetail = appWebUserDetails.getUser();
				}
			}
		} catch (Exception e) {
			log.debug("getLoginDetail() - " + e.getMessage(), e);
		}
		return loginDetail;
	}
    
    /**
     * get current vistar's user
     * 
     * @return
     */
    public static WebUserDetails getWebUserDetails() {
        try {
        	if (SecurityContextHolder.getContext() != null && SecurityContextHolder.getContext().getAuthentication() != null) {
	            Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
	            if (obj != null && obj instanceof WebUserDetails) {
	                return (WebUserDetails) obj;
	            }
        	}
        } catch (Exception e) {
            log.debug("getWebUserDetails() - " + e.getMessage(), e);
        }
        return null;
    }

    /**
     * check current user is annonymous or not
     * 
     * @return
     */
    public static boolean isAnonymous() {
        AuthenticationTrustResolver resolver = new AuthenticationTrustResolverImpl();
        SecurityContext ctx = SecurityContextHolder.getContext();
        if (ctx != null) {
            Authentication auth = ctx.getAuthentication();
            return resolver.isAnonymous(auth);
        }
        return true;
    }

    /**
     * remove authentication
     * 
     * @return
     */
    public static void removeAuthentication() {
        try {
        	if (SecurityContextHolder.getContext() != null && SecurityContextHolder.getContext().getAuthentication() != null) {
        		SecurityContextHolder.getContext().getAuthentication().setAuthenticated(false);
        	}
        } catch (Exception e) {
            log.debug("getWebUserDetails() - " + e.getMessage(), e);
        }
    }
    
    /**
     * check current password
     * 
     * @return
     */
    public static boolean checkCurrentPass(String password) {
    	boolean res = false;
        try {
			WebUserDetails webUserDetails = ContextHelper.getWebUserDetails();
			if (webUserDetails != null && StringUtils.isNotBlank(password)) {
				AppWebUserDetails appWebUserDetails = (AppWebUserDetails) webUserDetails;
				if (appWebUserDetails != null && appWebUserDetails.getPassword() != null) {
					String curPW = appWebUserDetails.getPassword();
					System.out.println("curPW ==  " + curPW);
					if (StringUtils.isNotBlank(curPW) &&
							curPW.equals(EncryptionHelper.encodeMD5(password))) {
						res = true;
						System.out.println("passss equal");
					}
				}
			}
        } catch (Exception e) {
            log.debug("getWebUserDetails() - " + e.getMessage(), e);
        }
        return res;
    }
    
    /**
     * get current role
     * 
     * @return
     */
    public static String getCurrentRole() {
        try {
        	if (SecurityContextHolder.getContext() != null && SecurityContextHolder.getContext().getAuthentication() != null) {
	            Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
	            if (obj != null && obj instanceof WebUserDetails) {
	            	WebUserDetails webUD = (WebUserDetails) obj;
	            	return webUD.getAuthorities().get(0).getAuthority();
	            }
        	}
        } catch (Exception e) {
            log.debug("getWebUserDetails() - " + e.getMessage(), e);
        }
        return null;
    }
}